package com.example.WebTask1.Filter;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.io.IOException;

public class FilterTwo implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        HttpSession session = httpServletRequest.getSession();
//        String user = String.valueOf(session.getAttribute("user"));
        Object user = session.getAttribute("user");
        System.out.println(user);
        if (user!=null){
            System.out.println("第一个过滤");
            filterChain.doFilter(httpServletRequest,httpServletResponse);


        }else if (user==null&&httpServletRequest.getServletPath().contains("login")){
            System.out.println("第二个过滤");
            filterChain.doFilter(httpServletRequest,httpServletResponse);
        }else {
            System.out.println("第三个过滤");
            httpServletRequest.getRequestDispatcher("index.jsp").forward(httpServletRequest,httpServletResponse);
        }
    }
}
